<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$setting=$this->setting;
$member=$this->pdo_get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
$sale_mid=$_RQ['sale_mid'];
if($sale_mid){
	$update=array('sale_mid'=>$sale_mid);
	if($member['is_agent']){
		$share_link=$return['pluginset']['share_link']?:'jy_mfee/manage.index/';
		exi('您已是分销商！不能成为他人的直销用户！','error',$share_link);
	}else{
		$this->model('member')->member_update($_SESSION['uid'],$update);
	}
}
if($_RQ['footernav']){
	$return['pluginset']['footernav']=plugin($_RQ['footernav'])->plugin_setting('setting')['footernav'];
}
$mid=$_RQ['mid']?$_RQ['mid']:$_SESSION['uid'];
$sao_fromuser=$this->session('sao_fromuser');
if($_RQ['fromuser'] || $sao_fromuser){
		if($_RQ['fromuser'] && !$sale_mid && ($this->setting['become_child']==1 || $this->setting['become_child']==4)){
			$this->session('becomefromuser',$_RQ['fromuser']);
			$this->model('member')->checkmember('',false);
		}
		$_RQ['fromuser']=$_RQ['fromuser']?:$sao_fromuser;
		$team=$this->pdo_get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_RQ['fromuser'],'uniacid'=>$_SESSION['uniacid']));
		$teamlevel =$this->pdo_get('level',array('id'=>$team['level_id'],'uniacid'=>$_SESSION['uniacid']));
		$teamlevel['title'] = $teamlevel['title']?:'普通会员';
		//扫码成为下线
		if($_RQ['fromuser'] && ($this->setting['become_child']==4 || $this->setting['become_child']==1) && !$member['agent_id']){
			$this->model('member')->set_parent($_SESSION['uid'],$_RQ['fromuser']);
		}
		if($teamlevel['isyidong']==1 && $_RQ['fromuser'] && $member['agent_id']){
			$this->model('member')->set_parent($_SESSION['uid'],$_RQ['fromuser'],true);
		}
		if($teamlevel['set_group'] && $_RQ['fromuser']){
			model('jy_weishop/member')->up_member_level($_SESSION['uid'],$teamlevel['set_group'],$_SESSION['onemerchid']);
		}
		$this->session('sao_fromuser','');
}
if(($_RQ['fromuser'] || $sale_mid) && $return['pluginset']['share_link']){
	exi('','',$return['pluginset']['share_link']);
}
if($_RQ['fromuser'] && $_SESSION['current_plugin']!='jy_mfee'){
	$nowplugin=$_SESSION['current_plugin']=='core'?'jy_weishop':$_SESSION['current_plugin'];
	$menus_index=$_SESSION['menus_index']?:$nowplugin.'/index/';
	if($_SESSION['onemerchid']){
		$menus_index='jy_merch/myshop//id='.$_SESSION['onemerchid'];
	}
	exi('','',$menus_index);
}
if($_RQ['op']=='main'){
	$myshop=pdo_get('jy_mfee_shop',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$sale_mid?:$mid,'uniacid'=>$_SESSION['uniacid']));
	if(!$myshop['mid']){
		$res=$this->model('member')->check_myshop($mid);
		if(!$res){
			//exi('该用户还没有小店！','error','jy_weishop/index/');
		}
	}
	$return['mid']=$mid;
	$return['sharedata']=array(
			'title'=>$myshop['title']?$myshop['title']:'我的小店',
			'desc'=>$myshop['desc']?:$myshop['title'].'描述',
			'link'=>custom_url('jy_mfee/myshop/main/mid='.$mid.'&onemerchid='.$_SESSION['onemerchid'],true),
			'imgUrl'=>tomedia($myshop['logo']),
	);
	$return['member']=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('id','avatar','nickname'));
	$myshop['logo']=$myshop['logo']?$myshop['logo']:$return['member']['avatar'];
	$return['setting']=$setting;

	$level=pdo_getcolumn('jy_mfee_member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']),'level_id');
	if($level){
		$return['level']=pdo_getcolumn('jy_mfee_level',array('id'=>$level),'title');	
	}else{
		$return['level']='普通等级';
	}
	$return['ordercount']=$total['total'];
	$goodsids = explode(';',$myshop['goodsids']);
	if($myshop['selectgoods']){
		$goodsids = explode(';',$myshop['goodsids']);
		$params['id']=$goodsids;
		$return['goodscount']=count($goodsids)-2;
	}else{
		$return['goodscount']=pdo_count('jy_weishop_goods',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'status'=>1));
	}
	$myshop['title'] = $myshop['title']?$myshop['title']:$return['member']['nickname'].'的小店';
	$myshop['logo'] = $myshop['logo']?tomedia($myshop['logo']):$return['member']['avatar'];
	$myshop['img'] = $myshop['img']?tomedia($myshop['img']):'';
	$image=ROOT_AT.'images/jy_mfee/'.$_SESSION['uniacid'].'/i'.$_SESSION['onemerchid'].'d'.$mid.'.jpg';
	if(!file_exists($image)){
		$url='/jy_mfee/myshop/index?mid='.$mid.'&fromuser='.$_SESSION['uid'].'&onemerchid='.$_SESSION['onemerchid'];
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
		$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
		$path=ROOT_AT.'images/jy_mfee/'.$_SESSION['uniacid'];
		if(!file_exists($path)){
				mkdir($path,0700, true);
		}
		file_put_contents($path.'/id'.$mid.'.jpg',$res);
	}
	$myshop['appcode'] = 'images/jy_mfee/'.$_SESSION['uniacid'].'/i'.$_SESSION['onemerchid'].'d'.$mid.'.jpg';
	$myshop['title1']=cfc('tools')->substr($myshop['title'],0,13);
	$myshop['title2']=cfc('tools')->substr($myshop['desc'],0,13);
	$myshop['title2']=$myshop['title2']?$myshop['title2'].'...':'暂无描述';
	$return['myshop']=$myshop;
	$return['params']=array('page'=>1);
	set_html('title',$myshop['title']);
	exi($return);
}
if($_RQ['op']=='list'){
	$myshop=pdo_get('jy_mfee_shop',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$sale_mid?:$mid,'uniacid'=>$_SESSION['uniacid']));
	$return['params']=array();
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'status'=>1);
	if($myshop['selectgoods']){
		$goodsids = explode(';',$myshop['goodsids']);
		$params['id']=$goodsids;
	}
	if($_RQ['keyword']){
		if(is_numeric($_RQ['keyword'])){
			$params['id']=intval($_RQ['keyword']);
			$return['params']['id']=intval($_RQ['keyword']);
		}else{
			$params['title like']='%'.trim($_RQ['keyword']).'%';
			$return['params']['title']=$_RQ['keyword'];
		}
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$parameter=array(
			'params'=>$params,
			'limit'=>array($pindex,$psize),
			'fields'=>$fields,
			'orderby'=>$orderby,
	);
	$list=model('jy_weishop/goods')->goods_list($parameter,$total);
	if(!$list){
		exi('none');
	}
	if($list){
		$install = check_permission('jy_merch');
		foreach($list as &$itemv){
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//按点赞数排序作品
			$pindex = max(1, 0);
			$psize = 4;
			$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),array($pindex,$psize),$total,array('mid'),'','id desc');
			foreach($members as &$mv){
				$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
				$mv['nickname']=$memberinfo['nickname'];
				$mv['avatar']=$memberinfo['avatar'];
			}
			$itemv['members']=$members;
			
			if($install){
				$itemv['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$itemv['merchid']),'merchname');
			}
			$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],),'id');
			$itemv['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$itemv['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'total');
			$itemv['cartid']=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$itemv['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
		}
	}
	exi($list);
}
?>